package com.yj.daily.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summarizingInt;

/**
 * Stream 拆分 list
 */
public class Demo03 {


    static List<TestPerson> persons = Arrays.asList(
            new TestPerson("zhangsan",1,20),
            new TestPerson("lisi",2,21),
            new TestPerson("wangwu",3,20),
            new TestPerson("wuyif",3,20)
    );


    public static void main(String[] args) {
        // 根据id分组
        Map<Integer, List<TestPerson>> map = persons.stream().collect(groupingBy(TestPerson::getId));
        System.out.println(map);

        Map<Integer, IntSummaryStatistics> personPerAge = persons.stream().collect(groupingBy(TestPerson::getId, summarizingInt(TestPerson::getAge)));
        System.out.println(personPerAge);
    }
}

class TestPerson{

    String userName;
    int id;
    int age;

    public TestPerson(String userName, int id, int age) {
        this.userName = userName;
        this.id = id;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "TestPerson{" +
                "userName='" + userName + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }

}
